<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE 打字机效果</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .output {
            min-height: 100px;
            border: 1px solid #ddd;
            padding: 20px;
            margin: 20px 0;
            border-radius: 5px;
            background-color: #fafafa;
            font-size: 18px;
            line-height: 1.6;
        }
        .controls {
            margin: 20px 0;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .status {
            margin-top: 10px;
            font-style: italic;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>SSE 打字机效果演示</h1>
        <div class="controls">
            <button id="startBtn">开始</button>
            <button id="stopBtn" disabled>停止</button>
            <button id="clearBtn">清空</button>
        </div>
        <div class="output" id="output"></div>
        <div class="status" id="status">准备就绪</div>
    </div>

    <script>
        let eventSource = null;
        const output = document.getElementById('output');
        const status = document.getElementById('status');
        const startBtn = document.getElementById('startBtn');
        const stopBtn = document.getElementById('stopBtn');
        const clearBtn = document.getElementById('clearBtn');

        function startSSE() {
            if (eventSource) {
                eventSource.close();
            }
            
            eventSource = new EventSource('http://127.0.0.1:8891/sse');
            status.textContent = '连接中...';
            startBtn.disabled = true;
            stopBtn.disabled = false;

            eventSource.onopen = function() {
                status.textContent = '已连接，正在接收数据...';
            };

            eventSource.onmessage = function(event) {
                // 实现打字机效果
                const char = event.data;
                typeWriter(char);
            };

            eventSource.onerror = function(err) {
                console.error("EventSource failed:", err);
                status.textContent = '连接错误';
                stopSSE();
            };
        }

        function stopSSE() {
            if (eventSource) {
                eventSource.close();
                eventSource = null;
            }
            status.textContent = '已停止';
            startBtn.disabled = false;
            stopBtn.disabled = true;
        }

        function clearOutput() {
            output.textContent = '';
            status.textContent = '已清空';
        }

        // 打字机效果实现
        function typeWriter(char) {
            output.textContent += char;
            
            // 自动滚动到底部
            output.scrollTop = output.scrollHeight;
        }

        // 绑定事件
        startBtn.addEventListener('click', startSSE);
        stopBtn.addEventListener('click', stopSSE);
        clearBtn.addEventListener('click', clearOutput);

        // 页面卸载时关闭连接
        window.addEventListener('beforeunload', function() {
            if (eventSource) {
                eventSource.close();
            }
        });
    </script>
</body>
</html>
